package osb.map.database;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import osb.map.publicdata.Public_Operate_And_Data;
import android.app.Activity;
import android.content.Context;


/**
 * 配置文件读写
 * @author Administrator
 *
 */

/*
 * 本软用到
 * 是否记住密码     1
 * 账户                       2
 * 界面风格              3
 * 姓名                       4
 * 余额                       5
 */

public class ConfigurationRW extends Activity
{
	private static String static_IsRecordPsw;   // 是否记住密码  1
	private static String static_UserId;        // 账户 2
	private static String static_UIStype;       // 界面风格 3
	private static String static_UserName;      // 姓名4
	private static String static_UserAccout;    // 余额  5
	
	//写入  ----本软件的专门写入    3个信息      是否记住密码/账户/界面风格       用覆盖写法
	//ConfigurationRW.Configuration_W_this("true", Public_Operate_And_Data.User_id, null,Public_Operate_And_Data.User_name,""+Public_Operate_And_Data.User_accout, this);   //将用户ID写入配置文件
	public static void Configuration_W_this(String IsRecordPsw,String UserId,String UIStype,String UserName,String UserAccout,Context context)    //配置信息     标志位   对象指针
	{
		
		if(IsRecordPsw!=null)
		{
			static_IsRecordPsw=IsRecordPsw;
		}
		else {
			if(Public_Operate_And_Data.IsAutoLogin)
			{
				static_IsRecordPsw = "true";
			}
			else {
				static_IsRecordPsw = "false";
			}
		}
		
		if(UserId!=null)
		{
			static_UserId=UserId;
		}
		else {
			static_UserId = Public_Operate_And_Data.User_id;
		}
		
		if(UIStype!=null)
		{
			static_UIStype=UIStype;
		}
		else {
			static_UIStype = "" + Public_Operate_And_Data.UI_Stype_INT_Curren;
		}
		
		if(UserName!=null)
		{
			static_UserName=UserName;
		}
		else {
			static_UserName = "" + Public_Operate_And_Data.User_name;
		}
		
		if(UserAccout!=null)
		{
			static_UserAccout=UserAccout;
		}
		else {
			static_UserAccout = "" + Public_Operate_And_Data.User_accout;
		}
		
		 LocaleConfiguration localeConfiguration=new LocaleConfiguration();
		 context.getFileStreamPath(PREFERENCES).delete();  //每次写入都先删除
		 if(static_IsRecordPsw!=null)
			{
			 localeConfiguration.locale=static_IsRecordPsw;
			 localeConfiguration.mcc=0;
			 localeConfiguration.mnc=1;
			 writeConfiguration(context,localeConfiguration);
			}
			if(static_UserId!=null)
			{
				 localeConfiguration.locale=static_UserId;
				 localeConfiguration.mcc=0;
				 localeConfiguration.mnc=2;
				 writeConfiguration(context,localeConfiguration);
			}
			if(static_UIStype!=null)
			{
				 localeConfiguration.locale=static_UIStype;
				 localeConfiguration.mcc=0;
				 localeConfiguration.mnc=3;
				 writeConfiguration(context,localeConfiguration);
			} 
			if(static_UserName!=null)
			{
				 localeConfiguration.locale=static_UserName;
				 localeConfiguration.mcc=0;
				 localeConfiguration.mnc=4;
				 writeConfiguration(context,localeConfiguration);
			}  
			if(static_UserAccout!=null)
			{
				 localeConfiguration.locale=static_UserAccout;
				 localeConfiguration.mcc=0;
				 localeConfiguration.mnc=5;
				 writeConfiguration(context,localeConfiguration);
			}  
	}	 
	
	
	//写入
	//调用办法
	//ConfigurationRW.Configuration_W("aaaa",0,this);
	//ConfigurationRW.Configuration_W("bbbb",1,this);
	//ConfigurationRW.Configuration_W("cccc",2,this);
	//ConfigurationRW.Configuration_W("dddd",3,this);
	public static void Configuration_W(String str_configur,int sign,Context Context)    //配置信息     标志位   对象指针
	{
		 LocaleConfiguration localeConfiguration=new LocaleConfiguration();
		 localeConfiguration.locale=str_configur;
		 localeConfiguration.mcc=0;
		 localeConfiguration.mnc=sign;
	     writeConfiguration(Context,localeConfiguration);
	}
	//读取
    //ConfigurationRW.Configuration_R(1,this);
	public static String Configuration_R(int sign,Context Context)     //标志位    ，对象指针         
	{
		 LocaleConfiguration localeConfiguration=new LocaleConfiguration();	 
		 return readConfiguration(Context,localeConfiguration,sign);   //输出   返回     返回NULL表示没有找到  或  信息本身为空
	}
	private static final String PREFERENCES = "ZhongDaXia.preferences";    //配置文件存储名称
	
	private static class LocaleConfiguration
	{
		public String locale;      
		public int mcc = -1;      
		public int mnc = -1;
	}

	private static void writeConfiguration(Context context,
			LocaleConfiguration configuration)
	{
		
		DataOutputStream out = null;
		try
		{
			out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_APPEND)); // MODE_PRIVATE 覆盖写入       MODE_APPEND--追加
			out.writeUTF(configuration.locale);
			out.writeInt(configuration.mcc);
			out.writeInt(configuration.mnc);
			out.flush();
		} catch (FileNotFoundException e)
		{
			// Ignore
		} catch (IOException e)
		{
			// noinspection ResultOfMethodCallIgnored
			context.getFileStreamPath(PREFERENCES).delete();
		} finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
					// Ignore
				}
			}
		}
	}

	private static String readConfiguration(Context context,
			LocaleConfiguration configuration,int sign)   //对象     类对象   标志位
	{
		DataInputStream in = null;
		try
		{
			in = new DataInputStream(context.openFileInput(PREFERENCES));

			while (in != null)
			{
				configuration.locale = in.readUTF();
				configuration.mcc = in.readInt();
				configuration.mnc = in.readInt();

				if (configuration.mcc == 0 && (configuration.mnc == sign))
				{
					return configuration.locale;  //找到信息    输出
				}
			}

		} catch (FileNotFoundException e)
		{

		} catch (IOException e)
		{

		} finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				} catch (IOException e)
				{
		
				}
			}
		}
		return null ;
	}
}
